package com.xyauto.theme.network.interceptor

import com.blankj.utilcode.util.NetworkUtils
import com.xyauto.theme.BuildConfig
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException


/**
 * Created by gongziyi on 2023/4/3 10:36.
 */
class OfflineCacheInterceptor : Interceptor {

    /**在线缓存时间 单位秒 debug状态下为10秒，release下为1天*/
    private val maxAge = /*if (BuildConfig.DEBUG) 60 * 5 else */60 * 60 * 24

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val response = chain.proceed(chain.request())
        if (isOnline()) {
            //否则添加默认缓存策略
            return response.newBuilder()
                .removeHeader("Pragma")
                .removeHeader("Cache-Control")
                .header("Cache-Control", "Public, max-age=$maxAge")
                .build()
        }
        return response
    }

    private fun isOnline(): Boolean {
        // 判断是否有网络连接
        // 这里可以根据具体情况来实现
        return NetworkUtils.isConnected()
    }
}